import os
import collections

def work(path):
    resPath = r"G:\python学习课件\python学习笔记\千锋教育\day\day10\res"
    # 打开文件
    with open(path, "r") as f:
        while True:
            # laphae11985@163.com----198587
            lineInfo = f.readline()
            if len(lineInfo) < 5:
                break
            # 邮箱的字符串
            # laphae11985@163.com
            emailStr = lineInfo.split("----")[0]
            # 邮箱类型的目录
            # G:\python学习课件\python学习笔记\千锋教育\day\day10\res\163
            fileType = emailStr.split("@")[1].split(".")[0]
            dirStr = os.path.join(resPath, fileType)
            if not os.path.exists(dirStr):
                # 不存在，创建
                os.mkdir(dirStr)
            filePath = os.path.join(dirStr, fileType + ".txt")
            with open(filePath, "w") as fw:
                fw.write(emailStr + "\n")

def getAllDirQU(path):
    queue = collections.deque()
    queue.append(path)
    while len(queue) != 0:
        dirPath = queue.popleft()
        fileList = os.listdir(dirPath)
        for fileName in fileList:
            fileAbsPath = os.path.join(dirPath, fileName)
            if os.path.isdir(fileAbsPath):
                queue.append(fileAbsPath)
            else:
                # 处理普通文件
                work(fileAbsPath)

getAllDirQU(r"G:\python学习课件\python学习笔记\千锋教育\day\day10\data")
